package com.crm.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.crm.entity.Role;

public interface RoleRepository extends JpaRepository<Role, Integer>,JpaSpecificationExecutor<Role>{

	/**
	 * 查询用户拥有的角色
	 * @param id
	 * @return
	 */
	@Query(value="SELECT * FROM role where role_id not in(SELECT r.role_id from role r,user_role u where u.role_id=r.role_id and u.user_id=?1) ", nativeQuery=true)
	List<Role> getRoleByUserId(String id);
	
	
	
	
	/**
	 * 添加角色和模块的关系
	 * @param rid
	 * @param mid
	 * @return
	 */
	@Transactional
    @Modifying
    @Query(value = "INSERT INTO `crm`.`role_module` (`role_id`, `module_id`) VALUES (?1, ?2)  ",nativeQuery = true)
    Integer addRole_Module(Integer rid,Integer mid);
	
	/**
	 * 删除角色和模块的关系
	 * @param rid
	 * @param mid
	 * @return
	 */
	@Transactional
    @Modifying
    @Query(value = "delete  from role_module where role_id=?1    ",nativeQuery = true)
    Integer delRole_Module(Integer rid);
}
